import {ElMessage} from "element-plus";
import {getBigIconUrl} from "../network/utils/ForumIconUtils.ts";

export const setWindowTitle = (title: string) => document.title = `${title} - NGA_LOCAL`

export const todo = (message?: string | undefined) => ElMessage.warning(message || "功能开发中...")

export const copyText = (text: string | number, type ?: string) => {
    console.log("复制", text)
    navigator.clipboard.writeText(text.toString());
    if (type) {
        ElMessage.success(`复制成功：${type}`);
    }
}

// 复制使用的主题代码
export const topicCode = (tid: number, title?: string | undefined) => {
    const tt = title?.replace(/\[/g, "<")?.replace(/]/g, ">") || `主题 ${tid}`
    return `[tid=${tid}]${tt}[/tid]`
}

export const topicUrl = (tid: number) => `https://bbs.nga.cn/read.php?tid=${tid}`

export const replyUrl = (pid: number, to?: number | undefined) => `https://bbs.nga.cn/read.php?pid=${pid}` + (to ? '&to=1' : '')

// 将任意值尝试转换为数字，如果转换失败则返回undefined
export const toNumber = (v: any | undefined | null): number | undefined => {
    if (v === undefined || v === null) return undefined
    const number = Number(v)
    return isNaN(number) ? undefined : number
}

// 版面或合集图标地址
export const iconUrl = (id: number | undefined): string => getBigIconUrl(id)?.replace('https://img4.nga.178.com/', '/static/res/') || ''